package com.bknow.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.bknow.mapper.UserMapper;
import com.bknow.model.PageBean;
import com.bknow.model.UserInfo;
import com.bknow.util.Md5Util;

@Service
public class UserService {

	@Autowired
	private UserMapper userMapper;

	// 查询所有用户
	public PageBean<List<UserInfo>> findAllUser(int page, int limit) {
		PageBean<List<UserInfo>> pb = new PageBean<>(page, limit);
		int total = userMapper.findUserCount();
		pb.setTotal(total);
		pb.setProducts(userMapper.findUserBystartLimit(pb.getStart(), limit));
		return pb;
	}

	public UserInfo isUserExist(String uName) {
		return userMapper.isUserExist(uName);
	}

	// 通过用户名密码查询用户
	public UserInfo findUserByNameAndPwd(UserInfo userInfo) {
		return userMapper.findUserByNameAndPwd(userInfo);
	}

	public UserInfo isEmailExist(String uEmail) {
		return userMapper.isEmailExist(uEmail);
	}

	public UserInfo isPhoneExist(String uPhone) {
		return userMapper.isPhoneExist(uPhone);
	}

	public int regist(UserInfo userInfo) {
		return userMapper.regist(userInfo);
	}

	public UserInfo findUserByName(String uName) {
		// TODO Auto-generated method stub
		return userMapper.findUserByName(uName);
	}

	public int deleteUserByUid(Integer uId) {

		return userMapper.deleteUserByUid(uId);
	}

	public int modifyUser(UserInfo userInfo) {

		if (StringUtils.isEmpty(userInfo.getuId())) {
			userInfo.setuPwd(Md5Util.getMd5(userInfo.getuPwd()));
			return userMapper.insertUser(userInfo);
		} else {
			return userMapper.updateUser(userInfo);
		}

	}

	public int patchDeleteUser(String ids) {
		JSONObject jsStr = JSONObject.parseObject(ids);
		String str = (String) jsStr.get("ids");
		String[] arr = str.split(","); // 用,分割
		return userMapper.patchDeleteUser(arr);
	}

}
